#!/usr/bin/env python
# -*- coding:utf-8 -*-
# Author:Zhangcl
import threading,time
event = threading.Event()
def lighter():
    count = 0
    event.set() #一开始是设置标志位
    while True:
        if count > 4 and count < 10: #改成红灯
            event.clear() #清除标志位
            print('\033[1;31mred light is on... \033[0m')
        elif count > 10: #改成绿灯
            event.set() #设置标志位
            count = 0
        else:
            print('\033[1;32mgreen light is on...\033[0m')
        time.sleep(1)
        count += 1
def car(num):
    while True:
        if event.is_set(): #代表绿灯，判断是否设置了标志位
            print('car [%s] is running....' % num)
            time.sleep(1)
        else:
            print('car [%s] is watting....' %num)
            event.wait() #进行等待
light = threading.Thread(target=lighter)
light.start()
for i in range(5):
    car_obj = threading.Thread(target=car,args=(i,))
    car_obj.start()